/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.colswe.solestac.entity.Rol;

/**
 *
 * @author Edison Parra
 */
public class RoleDao {

    /**Search all roles in the system.
     * @param em EntityManager to manage the operation with the database.
     * @return The roles list. 
     */
    public List<Rol> searchAll(final EntityManager em) {
        Query q = em.createQuery("SELECT u FROM Rol u");
        return (List<Rol>) q.getResultList();
    }

    /**Search a rol by rol name.
     * @param rn The rolname given
     * @param em EntityManager to manage the operation with the database.
     * @return The rol that matches with the rolename given.
     */
    public Rol search(final String rn, final EntityManager em) {
        Query q = em.createQuery("SELECT u FROM Rol u WHERE u.name = :rn").setParameter("rn", rn);
        return (Rol) q.getSingleResult();
    }

    /**Create a role.
     * @param r New Role.
     * @param em EntityManager to manage the operation with the database.
     */
    public void create(final Rol r, final EntityManager em) {
        em.persist(r);
    }

    /**Update a role.
     * @param r Role with the new information
     * @param em EntityManager to manage the operation with the database.
     */
    public void update(final Rol r, final EntityManager em) {
        em.merge(r);
    }

    /**Delete a role.
     * @param r Role to delete.
     * @param em EntityManager to manage the operation with the database.
     */
    public void delete(final Rol r,final  EntityManager em) {
        em.remove(r);
    }
}
